/*
 * bomb.c
 *
 *  Created on: 19 févr. 2013
 *      Author: osimeoni
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>

#include "SDL.h"
#include "SDL_image.h"

#include "../include/bomb.h"
#include "../include/misc.h"

struct s_bomb {
	int xbomb, ybomb;
	SDL_Surface * lenght_img[4];
	int temp;
	int power;
	enum e_state current_state;
};

//SDLK_ESCAPE

t_bomb bomb_init(int bomb_number) {
	struct s_bomb *bomb = malloc(sizeof(*bomb));
	if (!bomb)
		error("Memory error");

	bomb->lenght_img[EXPLOSION] = load_image(IMG_BOMB_TTL1);
	bomb->lenght_img[SHORT] = load_image(IMG_BOMB_TTL2);
	bomb->lenght_img[MIDDLE] = load_image(IMG_BOMB_TTL3);
	bomb->lenght_img[BIG] = load_image(IMG_BOMB_TTL4);

	return bomb;
}

void bomb_free(t_bomb bomb) {
	assert(bomb);
	int j;
	for (j = 0; j < 4; j++)
		SDL_FreeSurface(bomb->lenght_img[j]);

	free(bomb);
}

int bomb_get_x(t_bomb bomb) {
	assert(bomb != NULL);
	return bomb->xbomb;
}

int bomb_get_y(t_bomb bomb) {
	assert(bomb != NULL);
	return bomb->ybomb;
}

void bomb_set_current_state(t_bomb bomb, enum e_state state) {
	assert(bomb);
	bomb->current_state = state;
}

void bomb_from_map(t_bomb bomb, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_BOMB) {
				bomb->xbomb = i;
				bomb->ybomb = j;
			}
		}
	}
}

void bomb_display(t_bomb bomb, SDL_Surface *screen) {
	assert(bomb);
	assert(bomb->lenght_img[bomb->current_state]);
	assert(screen);

	SDL_Rect place;
	bomb->xbomb = bomb->xbomb * SIZE_BLOC;
	bomb->ybomb = bomb->ybomb * SIZE_BLOC;

	SDL_BlitSurface(bomb->lenght_img[bomb->current_state], NULL, screen,
			&place);
}

